<template>
  <div class="login" v-loading="isloading">
    <el-form :model="form" :rules="rules" class="form" ref="gform">
      <img src="../../assets/common/login-logo.png" alt="" />
      <el-form-item prop="user">
        <el-input
          placeholder="username"
          v-model="form.user"
          prefix-icon="el-icon-user-solid"
        ></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="password"
          show-password
          prefix-icon="el-icon-lock"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button class="btn" @click="login">登录登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 调用登录方法
// import { loginAPI } from '../../api/user'
// 导入辅助函数
// 调用校验方法
import { validpassword, validuser } from '../../utils/validate'
export default {
  data () {
    return {
      form: {
        user: '13800000002',
        password: '123456'
      },
      rules: {
        user: [
          { required: true, message: '手机号不能为空', triggle: 'blur' },
          {
            validator: validuser
          }
        ],
        password: [
          { required: true, message: '密码不能为空', triggle: 'blur' },
          {
            validator: validpassword
          }
        ]
      },
      isloading: false
    }
  },
  methods: {
    login () {
      this.$refs.gform.validate(async result => {
        try {
          // 调用Vuex里的登录的方法
          this.isloading = true
          await this.$store.dispatch('user/login', {
            mobile: this.form.user,
            password: this.form.password
          })
          // 跳转首页
          this.$router.push('/dashboard')
        } catch {
          console.log('登录失败')
        } finally {
          this.isloading = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-image: url('../../assets/common/login.jpg');
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  .form {
    img {
      margin-bottom: 50px;
    }
    ::v-deep .el-input__inner {
      background-color: #d4e4ff;
      height: 50px;
    }
    ::v-deep .el-form-item__error {
      color: white;
    }
    .btn {
      width: 100%;
      background-color: #407dff;
      border: none;
      height: 60px;
      color: white;
      font-size: 28px;
    }
    .el-form-item__error {
      color: white;
    }
  }
}
</style>
